package com.lawliet.example.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lawliet.example.model.system.SysPermission;
import com.lawliet.example.model.system.SysRole;
import com.lawliet.example.model.system.SysRolePermission;
import com.lawliet.example.model.system.vo.PermissionTreeVO;
import com.lawliet.example.model.vo.Result;
import com.lawliet.example.service.system.SysPermissionService;
import com.lawliet.example.service.system.SysRolePermissionService;
import com.lawliet.example.util.QueryHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;

@Tag(name = "SysPermission 接口")
@RestController
@RequestMapping("/api/sysPermission")
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;

    @Operation(summary = "权限列表")
    @GetMapping("/list")
    public Result<?> list(@Parameter(description = "查询条件") SysPermission data,
                          @Parameter(description = "当前页") @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                          @Parameter(description = "每页显示条数") @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req) {

        Page<SysPermission> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SysPermission> sysRoleQueryWrapper = QueryHelper.buildQueryWrapper(data);
        sysRoleQueryWrapper.eq("DEL_FLAG",0);//没有被删除的角色
        IPage<SysPermission> pageList = sysPermissionService.page(page, sysRoleQueryWrapper);

        return Result.OK(pageList);
    }

    @Operation(summary = "添加权限")
    @PostMapping("/add")
    public Result<?> add(@RequestBody SysPermission sysPermission) {
        sysPermissionService.save(sysPermission);
        return Result.OK("添加成功");
    }

    @Operation(summary = "编辑权限")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody SysPermission sysPermission) {
        sysPermissionService.updateById(sysPermission);
        return Result.OK("编辑成功");
    }

    @Operation(summary = "删除权限",description = "还需要删除子权限")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String ids) {

        sysPermissionService.deleteBach(ids);
        return Result.OK("删除成功");
    }

    @GetMapping("/tree")
    @Operation(summary = "获取权限树")
    public Result<?> getTree() {
        List<PermissionTreeVO> tree = sysPermissionService.getTree();
        return Result.OK(tree);
    }




    @Operation(summary = "根据用户id获取权限树")
    @GetMapping("/query/{userId}")
    public Result<SysPermission> queryById(@PathVariable Long userId) {
        return Result.OK(sysPermissionService.getById(userId));
    }
}
